import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_wan_android/arch/ui/toast.dart';
import 'package:flutter_wan_android/arch/util/sputil.dart';
import 'package:flutter_wan_android/event/event.dart';
import 'package:flutter_wan_android/event/event_bus.dart';
import 'package:flutter_wan_android/res/colors.dart';
import 'package:flutter_wan_android/res/theme.dart';
import 'package:flutter_wan_android/widget/wan_appbar.dart';

import 'package:provider/provider.dart';

import '../application.dart';
import '../r.dart';

class Setting extends StatefulWidget {
  @override
  _SettingState createState() => _SettingState();
}

class _SettingState extends State<Setting> {
  var _isDark = false;

  var _phoneInfo = "未知";

  MethodChannel platform = MethodChannel('bridge');

  @override
  void initState() {
    super.initState();
    _getPhoneInfo();

    SpUtil.getBool(WTheme.isDark).then((value) {
      setState(() {
        _isDark = value;
      });
    });
  }

  _getPhoneInfo() async {
    try {
      var result = await platform.invokeMethod('getInfo');
      setState(() {
        print(result);
        _phoneInfo = result;
      });
    } catch (e) {
      ToastUtil.show("e");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: WanAppbar(context, titleStr: "设置"),
      body: Container(
        color: wTheme.background,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              height: 50,
              width: double.infinity,
              color: wTheme.listItemBg,
              padding: EdgeInsets.only(left: 16, right: 16),
              child: Row(
                children: [
                  Expanded(
                    child: Text(
                      "深色模式",
                      style: TextStyle(fontSize: 16, color: wTheme.text1),
                    ),
                    flex: 1,
                  ),
                  Switch(
                      value: _isDark,
                      inactiveTrackColor: WColor.gray3,
                      onChanged: (value) {
                        setState(() {
                          _isDark = value;
                          _changeTheme();
                        });
                      }),
                ],
              ),
            ),
            Container(
              height: 50,
              color: wTheme.listItemBg,
              padding: EdgeInsets.only(left: 16, right: 16),
              child: Row(
                children: [
                  Expanded(
                    child: Text(
                      "系统信息",
                      style: TextStyle(fontSize: 16, color: wTheme.text1),
                    ),
                    flex: 1,
                  ),
                  Text(
                    _phoneInfo,
                    style: TextStyle(fontSize: 12, color: WColor.gray1),
                  ),
                  Image.asset(
                    R.imagesIcArraw,
                    width: 10,
                    height: 10,
                  )
                ],
              ),
            ),
            GestureDetector(
              onTap: () {
                _loginOut();
              },
              child: Container(
                height: 50,
                color: wTheme.listItemBg,
                padding: EdgeInsets.only(left: 16, right: 16),
                child: Row(
                  children: [
                    Expanded(
                      child: Text(
                        "退出登录",
                        style: TextStyle(fontSize: 16, color: wTheme.text1),
                      ),
                      flex: 1,
                    ),
                    Image.asset(
                      R.imagesIcArraw,
                      width: 10,
                      height: 10,
                    )
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  _loginOut() async {
    // context.read<WTheme>().switchTheme();

    var result = await showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text("提示"),
            content: Text("确认退出登录？"),
            actions: [
              GestureDetector(
                onTap: () {
                  Navigator.of(context).pop(0);
                },
                child: Text("取消",
                    style: TextStyle(fontSize: 16, color: WColor.black2)),
              ),
              Padding(padding: EdgeInsets.only(right: 8)),
              GestureDetector(
                onTap: () {
                  Navigator.of(context).pop(1);
                },
                child: Text(
                  "确认",
                  style: TextStyle(fontSize: 16, color: WColor.blue1),
                ),
              ),
              Padding(padding: EdgeInsets.only(right: 4)),
            ],
          );
        });

    // 点击确定退出登录后的逻辑
    if (result == 1) {
      Application.userInfo = null;
      SpUtil.put("userName", "");
      SpUtil.put("pwd", "");
      Application.hasLogin = false;
      Application.cookieJar.deleteAll();

      // 发送通知刷新页面
      EventBusUtil.getInstance().fire(WEvent.EVENT_LOGIN);
      Navigator.of(context).pop();

      ToastUtil.show("退出登录");
    }
  }

  _changeTheme() {
    context.read<WTheme>().switchTheme();
  }
}
